$(function() {
  var page = 1
  var pageSize = 5
  var imgs = []
  render()
  function render() {
    $.ajax({
      url: '/product/queryProductDetailList',
      type: 'GET',
      data: {
        page : page,
        pageSize : pageSize
      },
      success: function(info) {
        console.log(info)
        $('tbody').html(template('tmp',info))
        $('#paginator').bootstrapPaginator({
          bootstrapMajorVersion: 3,
          currentPage : page,
          totalPages : Math.ceil(info.total/info.size),
          onPageClicked:function(a,b,c,p) {
            page = p
            render()
          },
          itemTexts:function(type,page,current) {
            switch (type) {
              case 'first':
              return '首页'
              case 'prev':
              return '上一页'
              case 'next':
              return '下一页'
              case 'last':
              return '尾页'
              case 'page':
              return page
            }
          },
          tooltipTitles : function(type,page,current) {
            switch (type) {
              case 'first':
              return '首页'
              case 'prev':
              return '上一页'
              case 'next':
              return '下一页'
              case 'last':
              return '尾页'
              case 'page':
              return page
            }
          },
          useBootstrapTooltip: true,
        })
      }
    })
  }

  $('.addProduct').on('click',function() {
    $('#addModal').modal('show')
    $.ajax({
      url:'/category/querySecondCategoryPaging',
      type: 'get',
      data: {
        page : 1,
        pageSize : 100
      },
      success: function(info) {
        console.log(info)
        $('.dropdown-menu').html(template('tmp2',info))
      }
    })
  })

  $('.dropdown-menu').on('click','li',function() {
    $('.btn-text').html($(this).children().html())
    $('[name=brandId]').val($(this).data('id'))
    $('form').data('bootstrapValidator').updateStatus('brandId','VALID')
  })

  $('#file').fileupload({
    done:function(e,data) {
      if(imgs.length >= 3) {
        alert('请不要再上传了哦')
        return
      }
      $('.img_box').append('<img width="100" height="100" src="'+ data.result.picAddr + '">')
      console.log(data.result)
      imgs.push(data.result)
      if(imgs.length === 3) {
        $('form').data('bootstrapValidator').updateStatus('picStatus', 'VALID')
      }
      else {
        $('form').data('bootstrapValidator').updateStatus('picStatus', 'INVALID')
      }
    }
  })

  $('form').bootstrapValidator({
    excluded: [],
    // 配置的校验的小图标
    feedbackIcons: {
      valid: 'glyphicon glyphicon-thumbs-up',
      invalid: 'glyphicon glyphicon-thumbs-down',
      validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
      brandId: {
        validators: {
          notEmpty: {
            message: '请选择二级分类'
          }
        }
      },
      proName: {
        validators: {
          notEmpty: {
            message: '商品名称不能为空'
          }
        }
      },
      proDesc: {
        validators: {
          notEmpty: {
            message: '商品描述不能为空'
          }
        }
      },
      num: {
        validators: {
          notEmpty: {
            message: '商品库存不能为空'
          },
          regexp: {
            regexp: /^[1-9]\d{0,4}$/,
            message: '请输入有效的库存(1-99999)'
          }
        }
      },
      size: {
        validators: {
          notEmpty: {
            message: '商品尺寸不能为空'
          },
          regexp: {
            regexp: /^\d{2}-\d{2}$/,
            message: '请输入有效的尺码格式(xx-xx)'
          }
        }
      },
      oldPrice: {
        validators: {
          notEmpty: {
            message: '商品原价不能为空'
          }
        }
      },
      price: {
        validators: {
          notEmpty: {
            message: '商品价格不能为空'
          }
        }
      },
      picStatus: {
        validators: {
          notEmpty: {
            message: '请上传3张图片'
          }
        }
      },
    }
  })

  $('form').on('success.form.bv',function(e) {
    e.preventDefault()
    var param = $('form').serialize()
    param += "&picName1=" + imgs[0].picName + "&picAddr1=" + imgs[0].picAddr
    param += "&picName2=" + imgs[1].picName + "&picAddr2=" + imgs[1].picAddr
    param += "&picName3=" + imgs[2].picName + "&picAddr3=" + imgs[2].picAddr

    $.ajax({
      url:'/product/addProduct',
      type: 'post',
      data: param,
      success: function(info) {
        if(info.success) {
          $('#addModal').modal('hide')
          page = 1
          render()
          $('form').data('bootstrapValidator').resetForm(true)
          $('.btn-text').html('请选择二级分类')
          $('.img_box img').remove()
          imgs = []
          // console.log(imgs)
        }
      }
    })
  })
})